package com.market.goods.web;

import com.market.goods.constance.SuperRandom;
import com.market.goods.dao.es.Location;
import com.market.goods.dao.es.MarketGoodsDocument;
import com.market.goods.dao.mongo.MarketGoodsDetails;
import com.market.goods.dao.po.MarketGoodsPO;
import com.market.goods.dao.po.QueryJoinPO;
import com.market.goods.service.MarketGoodsService;
import com.market.goods.web.vo.MarketGoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Api(tags = "MarketGoods相关服务")
@RestController
@RequestMapping("/marketGoods")
public class MarketGoodsController {

    @Autowired
    MarketGoodsService marketGoodsService;

    //    中国经度范围:73°33′E至135°05′E;纬度范围:3°51′N至53°33′N。当中的字母E , 表示东经;字母N,表示北纬。东经正数,西经为负数;北纬为正数,南纬为负数
    @ApiOperation("insertSelective")
    @GetMapping("/insertSelective")
    public void insertSelective() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            for (int k = 0; k < 500000; k++) {
                String shopName = SuperRandom.getShopName();
                int zhengshu = (int) (Math.random() * (10000 + 1));
                int xiaoshu = (int) (Math.random() * (99 - 0 + 1) + 0);
                //纬度
                int latitudeZhengshu = (int) (Math.random() * (53 - 3 + 1) + 3);
                int latitudeXiaoshu = (int) (Math.random() * (100 - 0 + 1) + 0);
                //经度
                int longitudeZhengshu = (int) (Math.random() * (135 - 73 + 1) + 73);
                int longitudeXiaoshu = (int) (Math.random() * (100 - 0 + 1) + 0);

                String goodsPrice = zhengshu + "." + xiaoshu;
                String longitude = "";
//                if (longitudeZhengshu % 3 == 0) {
                longitude = longitudeZhengshu + "." + longitudeXiaoshu;
//                } else {
//                    longitude = "-" + longitudeZhengshu + "." + longitudeXiaoshu;
//                }
                String latitude = latitudeZhengshu + "." + latitudeXiaoshu;
                int createTime = SuperRandom.getNum(2017, 2020);
                Date time = SuperRandom.getTime(createTime);
//            String format = formatter.format(time);
//            log.info("addFormat={}",format);
                MarketGoodsPO marketGoodsPO = new MarketGoodsPO();
                marketGoodsPO.setShopId(UUID.randomUUID().toString().replace("-", ""));
                marketGoodsPO.setCreatedTime(time);
                marketGoodsPO.setUpdateTime(time);
                marketGoodsPO.setGoodsPrice(new BigDecimal(goodsPrice));
                marketGoodsPO.setLatitude(new BigDecimal(latitude));
                marketGoodsPO.setLongitude(new BigDecimal(longitude));
                marketGoodsPO.setShopName(shopName);
                int randomnum = SuperRandom.getNum(12, 22);
                for (int j = 0; j < randomnum; j++) {
                    long goodsTypeId = (long) (Math.random() * (999999999 - 0 + 1) + 0);
                    marketGoodsPO.setGoodsTypeId(goodsTypeId);
                    marketGoodsPO.setGoodsId(UUID.randomUUID().toString().replace("-", ""));
                    marketGoodsPO.setVersion(1);
                    String goodsName = SuperRandom.getCaiName();
                    String goodsLabels = SuperRandom.getGoodsLabels();
                    marketGoodsPO.setGoodsName(goodsName);
                    marketGoodsPO.setGoodsLabels(goodsLabels);

                    MarketGoodsDocument marketGoodsDocument = new MarketGoodsDocument();
                    MarketGoodsDetails marketGoodsDetails = new MarketGoodsDetails();
                    BeanUtils.copyProperties(marketGoodsPO, marketGoodsDocument);
                    BeanUtils.copyProperties(marketGoodsPO, marketGoodsDetails);
                    Location location = new Location();
                    location.setLat(marketGoodsPO.getLatitude());
                    location.setLon(marketGoodsPO.getLongitude());
//                List<BigDecimal> location = Lists.newArrayList();
//                location.add(marketGoodsPO.getLatitude());
//                location.add(marketGoodsPO.getLongitude());
                    marketGoodsDocument.setLocation(location);
//                    marketGoodsDetails.setLocation(location);
//                    .insertMarketGoodsDetailsForMongodb(marketGoodsDetails);
//                    log.info("MarketGoodsControllerMarketGoodsDocument={}", marketGoodsDocument);
//                    marketGoodsService.insertEs(marketGoodsDocument);
//                    marketGoodsService.insertSelective(marketGoodsPO);
                }
            }
        } catch (Exception e) {
            log.info("insertSelectiveException={}", e);
        }


    }

    @ApiOperation("insertSelective1")
    @GetMapping("/insertSelective1")
    public void insertSelective1() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            for (int k = 0; k < 500000; k++) {
                String shopName = SuperRandom.getShopName();
                int zhengshu = (int) (Math.random() * (10000 + 1));
                int xiaoshu = (int) (Math.random() * (99 - 0 + 1) + 0);
                //纬度
                int latitudeZhengshu = (int) (Math.random() * (53 - 3 + 1) + 3);
                int latitudeXiaoshu = (int) (Math.random() * (100 - 0 + 1) + 0);
                //经度
                int longitudeZhengshu = (int) (Math.random() * (135 - 73 + 1) + 73);
                int longitudeXiaoshu = (int) (Math.random() * (100 - 0 + 1) + 0);

                String goodsPrice = zhengshu + "." + xiaoshu;
                String longitude = "";
//                if (longitudeZhengshu % 3 == 0) {
                longitude = longitudeZhengshu + "." + longitudeXiaoshu;
//                } else {
//                    longitude = "-" + longitudeZhengshu + "." + longitudeXiaoshu;
//                }
                String latitude = latitudeZhengshu + "." + latitudeXiaoshu;
                int createTime = SuperRandom.getNum(2017, 2020);
                Date time = SuperRandom.getTime(createTime);
//            String format = formatter.format(time);
//            log.info("addFormat={}",format);
                MarketGoodsPO marketGoodsPO = new MarketGoodsPO();
                marketGoodsPO.setShopId(UUID.randomUUID().toString().replace("-", ""));
                marketGoodsPO.setCreatedTime(time);
                marketGoodsPO.setUpdateTime(time);
                marketGoodsPO.setGoodsPrice(new BigDecimal(goodsPrice));
                marketGoodsPO.setLatitude(new BigDecimal(latitude));
                marketGoodsPO.setLongitude(new BigDecimal(longitude));
                marketGoodsPO.setShopName(shopName);
                for (int j = 0; j < 12; j++) {
                    long goodsTypeId = (long) (Math.random() * (999999999 - 0 + 1) + 0);
                    marketGoodsPO.setGoodsTypeId(goodsTypeId);
                    marketGoodsPO.setGoodsId(UUID.randomUUID().toString().replace("-", ""));
                    marketGoodsPO.setVersion(1);
                    String goodsName = SuperRandom.getCaiName();
                    String goodsLabels = shopName + goodsName;
                    marketGoodsPO.setGoodsName(goodsName);
                    marketGoodsPO.setGoodsLabels(goodsLabels);

                    MarketGoodsDocument marketGoodsDocument = new MarketGoodsDocument();
                    BeanUtils.copyProperties(marketGoodsPO, marketGoodsDocument);
                    Location location = new Location();
                    location.setLat(marketGoodsPO.getLatitude());
                    location.setLon(marketGoodsPO.getLongitude());
//                List<BigDecimal> location = Lists.newArrayList();
//                location.add(marketGoodsPO.getLatitude());
//                location.add(marketGoodsPO.getLongitude());
                    marketGoodsDocument.setLocation(location);
//                    log.info("MarketGoodsControllerMarketGoodsDocument={}", marketGoodsDocument);
//                    marketGoodsService.insertEs(marketGoodsDocument);
                    marketGoodsService.insertSelective(marketGoodsPO);
                }
            }
        } catch (Exception e) {
            log.info("insertSelectiveException={}", e);
        }


    }

    @ApiOperation("insertSelective2")
    @GetMapping("/insertSelective2")
    public void insertSelective2() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            for (int k = 0; k < 500000; k++) {
                String shopName = SuperRandom.getShopName();
                int zhengshu = (int) (Math.random() * (10000 + 1));
                int xiaoshu = (int) (Math.random() * (99 - 0 + 1) + 0);
                //纬度
                int latitudeZhengshu = (int) (Math.random() * (53 - 3 + 1) + 3);
                int latitudeXiaoshu = (int) (Math.random() * (100 - 0 + 1) + 0);
                //经度
                int longitudeZhengshu = (int) (Math.random() * (135 - 73 + 1) + 73);
                int longitudeXiaoshu = (int) (Math.random() * (100 - 0 + 1) + 0);

                String goodsPrice = zhengshu + "." + xiaoshu;
                String longitude = "";
//                if (longitudeZhengshu % 3 == 0) {
                longitude = longitudeZhengshu + "." + longitudeXiaoshu;
//                } else {
//                    longitude = "-" + longitudeZhengshu + "." + longitudeXiaoshu;
//                }
                String latitude = latitudeZhengshu + "." + latitudeXiaoshu;
                int createTime = SuperRandom.getNum(2017, 2020);
                Date time = SuperRandom.getTime(createTime);
//            String format = formatter.format(time);
//            log.info("addFormat={}",format);
                MarketGoodsPO marketGoodsPO = new MarketGoodsPO();
                marketGoodsPO.setShopId(UUID.randomUUID().toString().replace("-", ""));
                marketGoodsPO.setCreatedTime(time);
                marketGoodsPO.setUpdateTime(time);
                marketGoodsPO.setGoodsPrice(new BigDecimal(goodsPrice));
                marketGoodsPO.setLatitude(new BigDecimal(latitude));
                marketGoodsPO.setLongitude(new BigDecimal(longitude));
                marketGoodsPO.setShopName(shopName);
                for (int j = 0; j < 12; j++) {
                    long goodsTypeId = (long) (Math.random() * (999999999 - 0 + 1) + 0);
                    marketGoodsPO.setGoodsTypeId(goodsTypeId);
                    marketGoodsPO.setGoodsId(UUID.randomUUID().toString().replace("-", ""));
                    marketGoodsPO.setVersion(1);
                    String goodsName = SuperRandom.getCaiName();
                    String goodsLabels = shopName + goodsName;
                    marketGoodsPO.setGoodsName(goodsName);
                    marketGoodsPO.setGoodsLabels(goodsLabels);

                    MarketGoodsDocument marketGoodsDocument = new MarketGoodsDocument();
                    BeanUtils.copyProperties(marketGoodsPO, marketGoodsDocument);
                    Location location = new Location();
                    location.setLat(marketGoodsPO.getLatitude());
                    location.setLon(marketGoodsPO.getLongitude());
//                List<BigDecimal> location = Lists.newArrayList();
//                location.add(marketGoodsPO.getLatitude());
//                location.add(marketGoodsPO.getLongitude());
                    marketGoodsDocument.setLocation(location);
//                    log.info("MarketGoodsControllerMarketGoodsDocument={}", marketGoodsDocument);
//                    marketGoodsService.insertEs(marketGoodsDocument);
                    marketGoodsService.insertSelective(marketGoodsPO);
                }
            }
        } catch (Exception e) {
            log.info("insertSelectiveException={}", e);
        }


    }


    @ApiOperation("qryMarketGoodsByShopId")
    @GetMapping("/qryMarketGoodsByShopId")
    public List<MarketGoodsPO> qryMarketGoodsByShopId(String shopId) {
        List<MarketGoodsPO> marketGoodsPOS = marketGoodsService.qryMarketGoodsByShopId(shopId);
        log.info("qryMarketGoodsByShopIdMarketGoodsPOS={}", marketGoodsPOS);
        return marketGoodsPOS;
    }

    @ApiOperation("qryMarketGoodsByGoodId")
    @GetMapping("/qryMarketGoodsByGoodId")
    public MarketGoodsPO qryMarketGoodsByGoodId(String goodId) {
        MarketGoodsPO marketGoodsPO = marketGoodsService.qryMarketGoodsByGoodId(goodId);
        log.info("qryMarketGoodsByGoodIdMarketGoodsPO={}", marketGoodsPO);
        return marketGoodsPO;
    }

    @ApiOperation("queryJoin")
    @GetMapping("/queryJoin")
    public List<QueryJoinPO> queryJoin(@RequestParam List<String> goodsIds) {
        List<QueryJoinPO> queryJoinPOS = marketGoodsService.queryJoin(goodsIds);
        log.info("queryJoinQueryJoinPOS={}", queryJoinPOS);
        return queryJoinPOS;
    }

    @ApiOperation("queryByEs")
    @PostMapping("/queryByEs")
    public List<MarketGoodsVO> queryByEs(@RequestBody MarketGoodsVO marketGoodsVO) {
        List<MarketGoodsVO> marketGoodsVOS = marketGoodsService.queryByEs(marketGoodsVO);
        log.info("queryByEsMarketGoodsVOS={}", marketGoodsVOS);
        return marketGoodsVOS;
    }


}
